﻿
import pygame
import sys
import random
import math
from pygame.locals import *


# 初始化所有 Pygame 模块
pygame.init()

# 设置窗口大小和标题
WINDOW_WIDTH = 800
WINDOW_HEIGHT = 600
screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption('Pygame 教程示例')

# 设置颜色
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)

# 设置时钟
clock = pygame.time.Clock()
FPS = 60  # 帧率

# 游戏循环标志
running = True






def draw_shapes():
    """绘制各种基本形状"""
    # 填充背景
    screen.fill(BLACK)
    
    # 绘制矩形
    pygame.draw.rect(screen, RED, (50, 50, 100, 100))
    pygame.draw.rect(screen, GREEN, (200, 50, 150, 75), 2)  # 边框
    
    # 绘制圆形
    pygame.draw.circle(screen, BLUE, (400, 100), 50)
    pygame.draw.circle(screen, YELLOW, (550, 100), 40, 3)  # 边框
    
    # 绘制椭圆
    pygame.draw.ellipse(screen, GREEN, (650, 50, 100, 50))
    
    # 绘制线条
    pygame.draw.line(screen, WHITE, (50, 200), (250, 200), 3)
    
    # 绘制多条线条
    points = [(300, 200), (350, 250), (400, 200), (450, 250)]
    pygame.draw.lines(screen, YELLOW, False, points, 2)
    
    # 绘制多边形
    polygon_points = [(500, 200), (550, 250), (600, 200), (550, 150)]
    pygame.draw.polygon(screen, RED, polygon_points)
    
    # 绘制弧形
    pygame.draw.arc(screen, BLUE, (650, 150, 100, 100), 0, math.pi/2, 3)
    
    # 更新显示
    pygame.display.flip()

# 运行绘制示例
draw_shapes()
pygame.time.wait(2000)  # 显示2秒



class Player(pygame.sprite.Sprite):
    """玩家精灵类"""
    def __init__(self, x, y):
        super().__init__()
        self.image = pygame.Surface((50, 50))
        self.image.fill(RED)
        self.rect = self.image.get_rect()
        self.rect.center = (x, y)
        self.speed = 5
        self.health = 100
    
    def update(self, keys):
        """更新玩家位置"""
        if keys[K_LEFT]:
            self.rect.x -= self.speed
        if keys[K_RIGHT]:
            self.rect.x += self.speed
        if keys[K_UP]:
            self.rect.y -= self.speed
        if keys[K_DOWN]:
            self.rect.y += self.speed
        
        # 限制在屏幕内
        self.rect.left = max(0, self.rect.left)
        self.rect.right = min(WINDOW_WIDTH, self.rect.right)
        self.rect.top = max(0, self.rect.top)
        self.rect.bottom = min(WINDOW_HEIGHT, self.rect.bottom)
    
    def draw_health_bar(self, surface):
        """绘制血条"""
        health_width = 50 * (self.health / 100)
        pygame.draw.rect(surface, RED, (self.rect.x, self.rect.y - 10, health_width, 5))
        pygame.draw.rect(surface, WHITE, (self.rect.x, self.rect.y - 10, 50, 5), 1)

class Enemy(pygame.sprite.Sprite):
    """敌人精灵类"""
    def __init__(self, x, y):
        super().__init__()
        self.image = pygame.Surface((30, 30))
        self.image.fill(GREEN)
        self.rect = self.image.get_rect()
        self.rect.center = (x, y)
        self.speed_x = random.randint(-3, 3)
        self.speed_y = random.randint(-3, 3)
        self.direction_change_time = pygame.time.get_ticks()
    
    def update(self):
        """更新敌人位置"""
        # 随机改变方向
        current_time = pygame.time.get_ticks()
        if current_time - self.direction_change_time > 2000:  # 每2秒改变一次方向
            self.speed_x = random.randint(-3, 3)
            self.speed_y = random.randint(-3, 3)
            self.direction_change_time = current_time
        
        self.rect.x += self.speed_x
        self.rect.y += self.speed_y
        
        # 边界检查
        if self.rect.left < 0 or self.rect.right > WINDOW_WIDTH:
            self.speed_x *= -1
        if self.rect.top < 0 or self.rect.bottom > WINDOW_HEIGHT:
            self.speed_y *= -1

class Bullet(pygame.sprite.Sprite):
    """子弹精灵类"""
    def __init__(self, x, y, direction):
        super().__init__()
        self.image = pygame.Surface((10, 10))
        self.image.fill(YELLOW)
        self.rect = self.image.get_rect()
        self.rect.center = (x, y)
        self.speed = 10
        self.direction = direction  # 角度（弧度）
    
    def update(self):
        """更新子弹位置"""
        self.rect.x += self.speed * math.cos(self.direction)
        self.rect.y += self.speed * math.sin(self.direction)
        
        # 如果子弹离开屏幕，删除它
        if (self.rect.right < 0 or self.rect.left > WINDOW_WIDTH or
            self.rect.bottom < 0 or self.rect.top > WINDOW_HEIGHT):
            self.kill()